This page last changed on May 03, 2007 by scytacki.

Loading dependencies of JNI dynamic libraries.

For example you have a file ccsensor.dll and it needs GoIO.dll. The GoIO.dll is not a JNI library it is a library uised by ccsensor.dll.

Windows

On windows you can simply put both libraries in the java.library.path and then call loadLibray() on each one in the correct order. In this case that order is GoIO.dll first, and then the ccsensor.dll.

This works with webstart too. If you put both dlls in a jar and then use the <nativelib> tag of webstart you can call loadLibrary() and it works.

OS X

On OS X these files would be called: libccsensor.jnilib and libGoIO.dylib.

The best way to make this work is to create a application bundle. Then the reference from the jnilib to the dylib can be made relative to the bundle. This is done by setting the "install_name". (links??)

Alternatively, you can export the DYLD_LIBRARY_PATH variable and set it to a directory where the dylib is then the library can be found.

From Webstart:

My conclusion is that it is not possible using the nativelib mechanism of webstart. I believe this should be a bug. There should be a way to load these type of dependencies through webstart on a os x. Perhaps a special install_name, or extacting the libraries to a tmp directory and setting DYLD_.. to point to that directory.

So there are few hacks:

  • rebuild the dylib as a static library
  • perhaps extract the dylib and put it in a known place so you can call System.load("fullpath") I have not tried this but it might work.
  • perhaps rebuild the dylib as a jnilib and load it with System.loadLibrary()

There is a good general page about OS X JNI here:
http://developer.apple.com/documentation/Java/Conceptual/Java14Development/Core_APIs/chapter_6_section_4.html

Here is a good page about dynamic library linking in OSX:
http://qin.laya.com/tech_coding_help/dylib_linking.html

This page alludes to the fact that it works with webstart, but It isn't clear they tried it on os x:
http://www.javagaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=LWJGL;action=print;num=1085355038

Some notes:

Calling loadLibrary on the libGoIO.dylib does not work because it doesn't end with jnilib.
However renaming it to .jnilib and call loadLibrary still does not let it be found by the main jnilib. This might be because the install_name is still the old name. try "otool -L libGoIO.jnilib".
After renaming or moving the libGoIO.dylib, if you link with it the resulting library or exectuable will have a reference to it in the old place. This is because it extracts the "install_name" from the libGoIO.dylib and uses that for the reference. In otherwords moving or renaming the file doesn't change this name.

Two types of errors seem to indicate if the jnilib is a bundle or dylib:

  • unsatisfiedlinkerror: this happens if it is bundle. It won't crash the jvm
  • the jvm crashes and reports it can't find @executable_path/... this occurs when the jnilib is dylib and it can't find a dependent dylib.

When the ccsensor is loaded it crashes the vm because it can't find the GoIO dependency.

TimTam test...

load will work only with jnilibs (bundle or dylibs)
it has nothing to do with dylibs

you can use otool -L to inspect jnilib/dylibs in mac os x
also you can use nm tool (man nm)
install_name_tool could be useful too (man install_name_tool)
I don't think you have to install library path for jnilibs built as dylibs
at least I never explicitly set that path for jnilibs
also try to put libGoIO.dylib into the ~/lib folder and see what will happen

Posted by at Feb 10, 2005 21:55
Document generated by Confluence on Jan 27, 2014 16:56